package com.cat.model.biz.logic.impl;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alonew.db.dao.TxCommonDAO;
import com.cat.model.biz.logic.SystemLogic;
import com.cat.model.common.model.admin.AdminModuleVO;
import com.cat.model.common.model.admin.AdminRolePermissionVO;
import com.cat.model.common.model.admin.AdminRoleVO;
import com.cat.model.common.model.admin.AdminUser;
import com.cat.model.common.model.admin.AdminUserRoleVO;
import com.cat.model.dao.common.PlmCommDAO;
import com.cat.model.dao.mapper.ModelMapper;

@Service("systemLogic")
public class SystemLogicImpl implements SystemLogic {

	@Autowired
	private TxCommonDAO commonDAO;
	
	@Autowired
	private PlmCommDAO plmCommDAO;
	
	@Autowired
	private ModelMapper modelMapper;
	
	@Override
	public AdminUser insertAdminUser(AdminUser vo) throws SQLException {
		commonDAO.insert(vo);
		return vo;
	}

	@Override
	public AdminUser updateAdminUser(AdminUser vo, String... fields)
			throws SQLException {
		commonDAO.updateById(vo, fields);
		return vo;
	}

	@Override
	public int deleteAdminUser(int id) throws SQLException {
		AdminUser vo = new AdminUser();
		vo.setId(new Long(id));
		return commonDAO.deleteById(vo);
	}

	@Override
	public AdminUser getAdminUser(int id) {
		return modelMapper.getAdminUser(id);
	}
	
	@Override
	public AdminUser getAdminUserByName(String username) {
		return modelMapper.getAdminUserByName(username);
	}
	
	@Override
	public AdminModuleVO insertAdminModule(AdminModuleVO vo) throws SQLException {
		commonDAO.insert(vo);
		return vo;
	}

	@Override
	public AdminModuleVO updateAdminModule(AdminModuleVO vo, String... fields)
			throws SQLException {
		commonDAO.updateById(vo, fields);
		return vo;
	}

	@Override
	public int deleteAdminModule(long id) throws SQLException {
		AdminModuleVO vo = new AdminModuleVO();
		vo.setId(id);
		return commonDAO.deleteById(vo);
	}

	@Override
	public AdminModuleVO getAdminModule(long id) {
		return modelMapper.getAdminModule(id);
	}

	@Override
	public List<AdminModuleVO> getAllAdminModule(String sysCode) {
		if(sysCode==null){
			return modelMapper.getAllAdminModule();
		} else {
			return modelMapper.getAllAdminModuleBySysCode(sysCode);
		}
	}
	
	@Override
	public AdminRoleVO insertAdminRole(AdminRoleVO vo) throws SQLException {
		commonDAO.insert(vo);
		return vo;
	}

	@Override
	public AdminRoleVO updateAdminRole(AdminRoleVO vo, String... fields)
			throws SQLException {
		commonDAO.updateById(vo, fields);
		return vo;
	}

	@Override
	public int deleteAdminRole(long id) throws SQLException {
		AdminRoleVO vo = new AdminRoleVO();
		vo.setId(id);
		return commonDAO.deleteById(vo);
	}

	@Override
	public AdminRoleVO getAdminRole(long id) {
		return modelMapper.getAdminRole(id);
	}
	
	@Override
	public AdminRolePermissionVO getAdminRolePermission(long roleId) {
		return modelMapper.getAdminRolePermission(roleId);
	}
	
	@Override
	public int deleteAdminRolePermission(long id) throws SQLException {
		AdminRolePermissionVO vo = new AdminRolePermissionVO();
		vo.setId(id);
		return commonDAO.deleteById(vo);
	}
	
	@Override
	public AdminRolePermissionVO updateAdminRolePermission(AdminRolePermissionVO vo, String... fields)
			throws SQLException {
		commonDAO.updateById(vo, fields);
		return vo;
	}
	
	@Override
	public AdminRolePermissionVO insertAdminRolePermission(AdminRolePermissionVO vo) throws SQLException {
		AdminRolePermissionVO pervo = new AdminRolePermissionVO();
		pervo = getAdminRolePermission(vo.getRoleId());
		if(pervo != null){
			pervo.setPermission(vo.getPermission());
			updateAdminRolePermission(pervo, "permission");
		} else {
			commonDAO.insert(vo);
		}
		
		return vo;
	}
	
	@Override
	public AdminUserRoleVO insertAdminUserRole(AdminUserRoleVO vo) throws SQLException {
		commonDAO.insert(vo);
		return vo;
	}
	
	@Override
	public int deleteAdminUserRole(long id) throws SQLException {
		AdminUserRoleVO vo = new AdminUserRoleVO();
		vo.setId(id);
		return commonDAO.deleteById(vo);
	}
	
}
